from machine import Pin
from typing import Union

def set_pin_high(pin: Pin) -> None:
    """
    Set the specified GPIO pin to high level
    """
    try:
        pin.value(1)
    except OSError as e:
        raise OSError(f"Failed to set pin high: {e}")

def set_pin_low(pin: Pin) -> None:
    """
    Set the specified GPIO pin to low level
    """
    try:
        pin.value(0)
    except OSError as e:
        raise OSError(f"Failed to set pin low: {e}")

def read_pin_state(pin: Pin) -> int:
    """
    Read the current state of GPIO pin
    """
    return pin.value()

def toggle_pin(pin: Pin) -> int:
    """
    Toggle GPIO pin state
    """
    current_state = pin.value()
    new_state = 1 - current_state
    pin.value(new_state)
    return new_state
